package com.shelpe.service.simbaproxy.filter;

import org.apache.commons.lang.math.NumberUtils;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class APIInvokeMetricFilter extends ZuulFilter {

	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();

		return ctx.getRouteHost() != null;
	}

	@Override
	public Object run() {
		RequestContext context = RequestContext.getCurrentContext();
		if(context.containsKey("metric_apiName") && context.containsKey("metric_startTime")){
			String method = context.get("metric_apiName").toString();
			long start = NumberUtils.toLong(context.get("metric_startTime").toString());
			if(start > 0){
				log.info("{} invoke costs {} ms", method, System.currentTimeMillis() - start);
			}
		}
		return null;
	}

	@Override
	public String filterType() {
		return "post";
	}

	@Override
	public int filterOrder() {
		return 1;
	}

}
